home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group95b.txt / 000078_icon-group-sender _Sun Jul 9 10:26:11 1995.msg < prev    next >
Internet Message Format  |  1995-09-18  |  2KB

  1. Received: by cheltenham.cs.arizona.edu; Sun, 9 Jul 1995 18:17:55 MST
  2. From: Nick Williams <nmw@ios.com>
  3. Message-Id: <199507091426.KAA23919@ios.com>
  4. Subject: Closures in Icon? Is it too late for a new feature to creep in?
  5. To: icon-group@cs.arizona.edu
  6. Date: Sun, 9 Jul 1995 10:26:11 -0400 (EDT)
  7. X-Mailer: ELM [version 2.4 PL23]
  8. Mime-Version: 1.0
  9. Content-Type: text/plain; charset=US-ASCII
  10. Content-Transfer-Encoding: 7bit
  11. Content-Length: 1835      
  12. Errors-To: icon-group-errors@cs.arizona.edu
  13.  
  14.  
  15. One of the various features of LISP that I absolutely love is closures,
  16. or, more accurately, the ability to produce new functions dynamically.
  17. I'd like to see something like that in Icon, except that I'm not about
  18. to propose anything like LISP's eval or even true LISP closures, but how
  19. about this:
  20.  
  21. procedure TestClosure(var1, var2, ...)
  22. environment catch_this_var, and_this_one
  23.  
  24.   ...
  25. end
  26.  
  27. procedure MakeTestClosure()
  28. local catch_thisvar, and_this_one
  29.  
  30.   ...
  31.   return closure(TestClosure)
  32. end
  33.  
  34. The 'closure' function would make a copy of the 'catch_this_var' and
  35. 'and_this_one' descriptors and store them in a new b_proc structure
  36. (procedure header). The variables declared in the 'environment' clause
  37. are equivalent to static, and are initialized to &null unless the
  38. function is closed over inside a function which has local variables of
  39. the same name, or unless there are globals of those names with values
  40. other than &null; additionally, the closure() function could perhaps
  41. take extra arguments to initialize any remaining, non-caught closure
  42. variables. By not storing references to the variables closed over we get
  43. semantics very different from LISP's: not real closures, but better than
  44. nothing, and it would be easy to implement (a few mods to h/rstructs.h,
  45. the GC, a few other places and the 'closure' function I bet).
  46.  
  47. This scheme could probably be extended a bit so that multiple functions
  48. could share one 'environment' (by defining the 'environment' outside the
  49. procedure declaration and giving it a name, declaring the participation
  50. in that environment declaration in the procedure defs, and having
  51. closure() take multiple procedure arguments and so on).
  52.  
  53. Wouldn't Icon be more interesting this way? :)
  54.  
  55. Nick
  56.  
  57. PS: forgive my terminology, I know it's loose, but I'm not at home,
  58.     where I have all my comp. sci. books.
  59.  
  60. y comp. sci. books.
  61.